﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CHPM.IC.ServiceLocator;
using CHPM.PL.DesktopUI.Code.Validation;
using CHPM.PL.DesktopUI.Interfaces;
using CHPM.PL.DesktopUI.Presenters;
using CHPM.PL.DesktopUI.ServiceReference1;

namespace CHPM.PL.DesktopUI.UserControls
{
    public partial class EditPatient : UserControl, IEditPatient,INewPatient
    {
        private NewPatientValidator _newPatientValidator;
        private bool IsEditing = true;

        public EditPatient()
        {
            InitializeComponent();
            bCancel.CausesValidation = false;
            bCancel.Click += new EventHandler(bCancel_Click);
            _newPatientValidator = new NewPatientValidator(this.epEditPatient);
            //_mainService = serviceLocator.Resolve<IMainService>();

            //if (id > 0)
            //{
            //    _editPatientPresenter = new EditPatientPresenter(serviceLocator, this, _mainService.GetPatientById(id));
            //}
            //else
            //{
            //    _editPatientPresenter = new EditPatientPresenter(serviceLocator, this, new PATIENT());
            //}
            //tbAddress.Validating += AdressValidating;

            tbAddress.Validating += _newPatientValidator.AdressValidation;
            tbFullname.Validating += _newPatientValidator.FullNameValidation;
            tbPhone.Validating += _newPatientValidator.PhoneValidation;
            Load += FormLoadExt;
            Load += SetValues;
            bSave.Click += btnSave_Click;

            //tbPhone.GotFocus += tbPhone_GotFocus;
        }

        public EditPatient(bool newPatient)
        {
            InitializeComponent();
            IsEditing = !newPatient;
            bCancel.CausesValidation = false;
            bCancel.Click += new EventHandler(bCancel_Click);
            _newPatientValidator = new NewPatientValidator(this.epEditPatient);

            tbAddress.Validating += _newPatientValidator.AdressValidation;
            tbFullname.Validating += _newPatientValidator.FullNameValidation;
            tbPhone.Validating += _newPatientValidator.PhoneValidation;
            bSave.Click += btnSave_Click;
        }

        void bCancel_Click(object sender, EventArgs e)
        {
            this.Controls.Clear();
            formPresenter.ShowMainForm();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FullName = tbFullname.Text;
            Card = tCardNumber.Text;
            Address = tbAddress.Text;
            Phone = tbPhone.Text;
            Birthday = dtpBirthday.Value;
            EnterDate = dtpEnterDate.Value;
            Invalid = cbInvalid.SelectedIndex;
            TreatmentCourse = cbTreatmentCourse.SelectedIndex;
            Ambulatory = cbAmbulatory.SelectedIndex == 0;
            Sex = rbMale.Checked;
            Chornobyl = cbChornobyl.Checked;
            Citizen = cbCitizen.Checked;
            Sanatoriy = cbSanatoriy.Checked;

            if(!IsEditing)
            {
                OnUserSaving(this, null);
            }
            else
            {
                PatientSaving(this, null);  
            }
            Controls.Clear();
            formPresenter.ShowMainForm();
        }

        void SetValues(object sender, EventArgs e)
        {
            tbFullname.Text = FullName;
            tCardNumber.Text = Card;
            dtpBirthday.Value = Birthday;
            rbMale.Checked = Sex;
            rbFemale.Checked = !Sex;
            tbAddress.Text = Address;
            cbChornobyl.Checked = Chornobyl;
            cbCitizen.Checked = Citizen;
            cbSanatoriy.Checked = Sanatoriy;
            dtpEnterDate.Value = EnterDate;
            cbInvalid.SelectedIndex = Invalid;
            cbTreatmentCourse.SelectedIndex = TreatmentCourse;
            cbAmbulatory.SelectedIndex = Convert.ToInt32(false);
            tbPhone.Text = Phone;
        }


        #region Implementation of IEditPatient

        

        public string FullName { get; set; }
        public string Card { get; set; }
        public DateTime Birthday { get; set; }
        public bool Sex { get; set; }
        public string Address { get; set; }
        public bool Chornobyl { get; set; }
        public bool Citizen { get; set; }
        public bool Sanatoriy { get; set; }
        public DateTime EnterDate { get; set; }
        public int Invalid { get; set; }
        public int TreatmentCourse { get; set; }
        public bool Ambulatory { get; set; }
        public string Phone { get; set; }
        public FormPresenter formPresenter { get; set; }

        #endregion

        public event EventHandler PatientSaving;
        public event CancelEventHandler AdressValidating;

        public event EventHandler FormLoadExt;

        #region Implementation of IView

        public Control AsControl { get { return this; } }

        #endregion

        public event EventHandler<ConsoleCancelEventArgs> OnUserSaving;


        //private string _phoneFormat = "777-777-7777";                 //
        //private void tbPhone_Leave(object sender, EventArgs e)        //
        //{                                                             //
        //    tbPhone.Text = _phoneFormat;                              //
        //}                                                             //  for newPatient form
        //void tbPhone_GotFocus(object sender, EventArgs e)             //
        //{                                                             //
        //    tbPhone.Text = String.Empty;                              //
        //}                                                             //

        //private EditPatientPresenter _editPatientPresenter;
        // private IMainService _mainService;


        //public EditPatient(IServiceLocator serviceLocator)
    }
}
